package com.atguigu.gulimall.test;

import com.atguigu.gulimall.test.entity.Teacher;
import com.atguigu.gulimall.test.service.TeacherService;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.StringJoiner;
import java.util.function.Supplier;


@SpringBootApplication
public class GulimallTestApplication {

    public static void main(String[] args) {
//        ConfigurableApplicationContext run = SpringApplication.run(GulimallTestApplication.class, args);
//        UserService bean = run.getBean(UserService.class);
//        System.out.println("bean名称");
//        System.out.println(bean.getCurrentClassBeanName());

        //SpringApplication.run(GulimallTestApplication.class, args);


        // 先获取BeanDefinition对象，(BeanDefinition是用来描述一个Bean信息的对象)
        AbstractBeanDefinition beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
        beanDefinitionBuilder.setBeanClass(Teacher.class);

        // 获取Spring容器
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
        // 把上面的BeanDefinition对象存入容器中
        //annotationConfigApplicationContext.registerBeanDefinition("teacher",beanDefinitionBuilder);
        annotationConfigApplicationContext.registerBean(Teacher.class, new Supplier<Teacher>() {
            @Override
            public Teacher get() {
                Teacher teacher = new Teacher();
                teacher.setName("刘君");
                return teacher;
            }
        });
        // 刷新容器
        annotationConfigApplicationContext.refresh();


        //annotationConfigApplicationContext.getBean("teacher"); // 由于Spring注入默认Beanid的名称字母第一个小写
        Teacher teacher1 = annotationConfigApplicationContext.getBean(Teacher.class);
        Teacher teacher2 = annotationConfigApplicationContext.getBean(Teacher.class);
        System.out.println(teacher1==teacher2);

        Teacher teacher = annotationConfigApplicationContext.getBean("teacher", Teacher.class);
        System.out.println(teacher);
    }

}
